package com.graduationproject.examonline.scorePaper.service;

import com.graduationproject.examonline.answerPaper.entity.AnswerPaper;
import com.graduationproject.examonline.answerPaper.mapper.AnswerPaperMapper;
import com.graduationproject.examonline.examPaper.entity.ExamPaper;
import com.graduationproject.examonline.examPaper.mapper.ExamPaperMapper;
import com.graduationproject.examonline.question.entity.Question;
import com.graduationproject.examonline.question.mapper.QuestionMapper;
import com.graduationproject.examonline.scorePaper.entity.ScorePaper;
import com.graduationproject.examonline.scorePaper.mapper.ScorePaperMapper;
import com.graduationproject.examonline.student.entity.Student;
import com.graduationproject.examonline.student.mapper.StudentMapper;
import com.graduationproject.examonline.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ScorePaperService {

    @Autowired
    private ScorePaperMapper scorePaperMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private ExamPaperMapper examPaperMapper;
    @Autowired
    private AnswerPaperMapper answerPaperMapper;
    @Autowired
    private StudentMapper studentMapper;

    public Result submitAnswer(Map<String, Object> score_paper, Map<String, Object> answer_paper) {
        try {
            // 从score_paper取exam_paper_id
            // 用exam_paper_id找exam_paper表，取question_score_list和question_id_list
            // 用question_id_list找question表，取true_answer
            String exam_paper_id = (String) score_paper.get("exam_paper_id");
            List<ExamPaper> examPaperList = examPaperMapper.getExamPaperById(exam_paper_id);
            String[] questionScoreList = null;
            List<String> trueAnswerList = new ArrayList<String>();
            String tempStudentAnswerList = ((String) answer_paper.get("answer_list")).replace("-$$&&$$-", "__,,__,,__");
            String[] studentAnswerList = tempStudentAnswerList.split("__,,__,,__");
            List<String> answer_score_list = new ArrayList<String>();
            if (examPaperList.size() > 0) {
                String question_score_list = examPaperList.get(0).getQuestion_score_list();
                String[] question_id_list = examPaperList.get(0).getQuestion_id_list().split(",");
                questionScoreList = question_score_list.split(",");
                for (int i = 0; i < question_id_list.length; i++) {
                    List<Question> questionList = questionMapper.getQuestionById(question_id_list[i]);
                    if (questionList.size() > 0) {
                        String trueAnswer = questionList.get(0).getTrue_answer();
                        trueAnswerList.add(trueAnswer);
                    }
                }
            }
            for (int i = 0; i < studentAnswerList.length; i++) {
                String score = questionScoreList[i];
                String tempTrueAnswer = trueAnswerList.get(i).replace("-$$&&$$-", "__,,__,,__");
                String tempStudentAnswer = studentAnswerList[i].replace("-&&-", "__,,__,,__");
                String[] trueAnswer = tempTrueAnswer.split("__,,__,,__");
                String[] studentAnswer = tempStudentAnswer.split("__,,__,,__");
                boolean flag = true;
                if (trueAnswer.length == studentAnswer.length) {
                    for (int j = 0; j < trueAnswer.length && flag; j++) {
                        if (!Arrays.asList(trueAnswer).contains(studentAnswer[j])) {
                            flag = false;
                        }
                    }
                } else {
                    flag = false;
                }
                if (flag) {
                    answer_score_list.add(score);
                } else {
                    answer_score_list.add("0");
                }
            }
            double scoreTotal = 0;
            for (int i = 0; i < answer_score_list.size(); i++) {
                String score = answer_score_list.get(i);
                scoreTotal += Integer.parseInt(score);
            }
            answer_paper.put("answer_score_list", String.join(",", answer_score_list));
            answer_paper.put("total_score", scoreTotal);
            score_paper.put("score", scoreTotal);
            int a = answerPaperMapper.insertAnswerPaper(answer_paper);
            int b = scorePaperMapper.insertScorePaper(score_paper);
            return new Result(0, 200, "ok");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "插入失败");
        }
    }

    public Result getScoreInfoByStuIdAndExamId(String stu_id, String exam_id) {
        try {
            List<ScorePaper> scorePaperList = scorePaperMapper.getScoreInfoByStuIdAndExamId(stu_id, exam_id);
            if (scorePaperList.size() > 0) {
                return new Result(0, 200, "ok", scorePaperList.get(0));
            } else {
                return new Result(1, 200, "nothing");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }

    public Result getStuAnswerByExamId(String stu_id, String exam_id) {
        try {
            List<ScorePaper> scorePaperList = scorePaperMapper.getScoreInfoByStuIdAndExamId(stu_id, exam_id);
            if (scorePaperList.size() > 0) {
                String answer_paper_id = scorePaperList.get(0).getAnswer_paper_id();
                List<AnswerPaper> answerPaperList = answerPaperMapper.getAnswerPaperById(answer_paper_id);
                if (answerPaperList.size() > 0) {
                    return new Result(0, 200, "ok", answerPaperList.get(0));
                } else {
                    return new Result(1, 200, "nothing");
                }
            } else {
                return new Result(1, 200, "nothing");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }

    public Result getScoreByExamId(String exam_id) {
        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            List<ScorePaper> records = scorePaperMapper.getScoreInfoByExamId(exam_id);
            for (int i = 0; i < records.size(); i++) {
                ScorePaper sp = records.get(i);
                String stu_id = sp.getStu_id();
                String stu_name = null;
                String class_id = null;
                List<Student> students = studentMapper.findStudentById(stu_id);
                if (students.size() > 0) {
                    stu_name = students.get(0).getStu_name();
                    class_id = students.get(0).getClass_id();
                }
                Map<String, Object> data = new HashMap<String, Object>();
                data.put("score_paper_id",sp.getScore_paper_id());
                data.put("stu_name", stu_name);
                data.put("stu_class", class_id);
                data.put("stu_id", stu_id);
                data.put("exam_id",sp.getExam_id());
                data.put("answer_paper_id",sp.getAnswer_paper_id());
                data.put("exam_paper_id",sp.getExam_paper_id());
                data.put("score",sp.getScore());
                data.put("status",sp.getStatus());
                dataList.add(data);
            }
            return new Result(0, 200, "ok", dataList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }
}
